clear all;
close all;

Filenames={' '};

colors=jet(length(Filenames));

figure;
for fi=1:length(Filenames)
    filename=Filenames{fi};

    data_all=xlsread(filename,'E3:F402');
   
    V_all(fi)=data_all(:,1);
    I_all(fi)=data_all(:,2);
    V=data_all(:,1);
    I=data_all(:,2);
    
    CSC(fi)=getCSC(V,I);
    
    hold on;
    plot(data_all(:,1),data_all(:,2),'color',colors(fi,:),'linewidth',2);
    colormap winter
    hold on;
    
end

grid on;
set(gca,'FontName','Helvetica','Fontsize',20);
xlabel('Voltage (V)');
ylabel('Current (uA)');
legend({''});
%%

figure;
subplot(2,2,1);
plot(V);
grid on;

subplot(2,2,2);
plot(I);
grid on;

subplot(2,2,3);
plot(V,I);
grid on;

%% CSC vs. Concentration
Indexes1=[ ]; %Same GF concentration
Indexes2=[ ]; %Same CNT concentration
concentrations=[ ];
%0.726,0.754,1.28,1.36,1.6,2.03,2.98
figure;
hold on;
scatter(concentrations(Indexes1),CSC(Indexes1)/1000,50,'markeredgecolor','r','linewidth',2);
scatter(concentrations(Indexes2),CSC(Indexes2)/1000,50,'markeredgecolor','k','linewidth',2);
xlabel('Carbon %');
ylabel('Charge Storage Capacity (mC/cm2)');
set(gca,'FontName','Helvetica','Fontsize',20);
grid on;

% For GF
ConcentrationsGF=concentrations(Indexes1);
CSC_GF=CSC(Indexes1)/1000;
x=unique(ConcentrationsGF);
y=nan;
err=nan;
for i=1:length(x)
   Indexes=find(ConcentrationsGF==x(i));
   
   y(i)=nanmean(CSC_GF(Indexes));
   err(i)=nanstd(CSC_GF(Indexes));
    
end
errorbar(x,y,err,'linewidth',2,'color','r');

% For CNT
ConcentrationsCNT=concentrations(Indexes2);
CSC_CNT=CSC(Indexes2)/1000;
x=unique(ConcentrationsCNT);
y=nan;
err=nan;
for i=1:length(x)
   Indexes=find(ConcentrationsCNT==x(i));
   
   y(i)=nanmean(CSC_CNT(Indexes));
   err(i)=nanstd(CSC_CNT(Indexes));
    
end
errorbar(x,y,err,'linewidth',2,'color','k');

%%
%ExportDataExcel=[concentrations;CSC];

%%
function CSC=getCSC(V,I)
    Speed=0.1;%V/s
    Indexes=1:101;
    Area1=sum(abs(I(Indexes)))*0.01;

    Indexes=find(I(101:300)>0);
    IndexCrossing=Indexes(1)+100;
    Area2=sum(abs(I(101:IndexCrossing)))*0.01;

    Indexes=find(I(301:end)<0);
    
    if ~isempty(Indexes)
        IndexCrossing=Indexes(1)+300;
        Area3=sum(abs(I(IndexCrossing:end)))*0.01;
    else
        Area3=0;
    end
    Area=(Area1-Area2+Area3);

    CSC=(Area/Speed)/(pi*(0.03)^2); % uC/cm2
end

